if(params.fromDate == null || params.fromDate == '') {
	request.title = 'Search'
	request.view = 'transcationSearch.gsp'
	
	forward 'wydLayout.gsp'
	return
}
use(groovy.time.TimeCategory) {
int timeZoneHour = session.userTimeZoneHour as int
int timeZoneMinute = session.userTimeZoneMinute as int

def fromDateTime = new Date().parse('dd-MM-yyyy HH:mm', "$params.fromDate $params.fromTime")
fromDateTime = fromDateTime - timeZoneHour.hours - timeZoneMinute.minutes
def toDateTime = new Date().parse('dd-MM-yyyy HH:mm', "$params.toDate $params.toTime")
toDateTime = toDateTime - timeZoneHour.hours - timeZoneMinute.minutes

def tentities = datastore.execute {
	from TranscationE
	sort desc by createdTime
	where userId == session.user.id and createdTime > fromDateTime and createdTime < toDateTime
}
def tagTotal = [:]
def total = 0

def sb = new StringBuilder('')
sb += """
<div id="viewAllResults">
<p align="center">
	Total amount spent from ${fromDateTime.format('dd-MM-yyyy HH:mm')} to ${toDateTime.format('dd-MM-yyyy HH:mm')} : <b>totalTagAmount</b>
</p>
<div id="barChart-all"></div>
<table id="transcationHistory" class="table-2" width="100%" border="0" cellpadding="5" cellspacing="0" align="center">
<thead>	
	<tr class="table-2-header">
		<th class="table-2-cell" align="center" width="40px">S. No.</th>
		<th class="table-2-cell" align="right" width="40px">Id</th>
		<th class="table-2-cell" align="right" width="60px">Amount</th>
		<th class="table-2-cell" align="left">Description</th>
		<th class="table-2-cell" align="left">Tags</th>
		<th class="table-2-cell" align="left">Date & Time</th>
	</tr>
</thead>
<tbody>
"""

def count = 1
tentities.each { transcation ->
	def ttentities = datastore.execute {
		from TranscationTagE
		sort asc by name
		where transcationId == transcation.key.id and userId == session.user.id
	}
	def s = count % 2 == 0 ? 'table-2-even-row' : 'table-2-odd-row'
	def tags = new StringBuilder()
	ttentities.each { tt ->
		tags += "<a class=\"table-1-link\" href=\"transcationViewForTag.groovy?transcationTag=$tt.name\">#$tt.name</a> "
		if(tagTotal[tt.name] == null) {
			tagTotal[tt.name] = 0
		}
		tagTotal[tt.name] += transcation.amount
	}
	def amount = String.format("%.2f", transcation.amount)
	def time = transcation.createdTime + timeZoneHour.hours + timeZoneMinute.minutes
	def createdTime = time.format('dd-MM-yyyy HH:mm:ss')

	total += transcation.amount
	sb += """
		<tr class='$s'>
			<td class='table-2-cell' align="right">${count}</td>
			<td class='table-2-cell' align="right"><a class='table-2-link' href='transcationEdit.groovy?transcationKey=${transcation.key.id}'>$transcation.key.id</a></td>
			<td class='table-2-cell' align="right">$amount</td>
			<td class='table-2-cell' align="left">$transcation.description</td>
			<td class='table-2-cell' align="left">$tags</td>
			<td class="table-2-cell" align="left">$createdTime</td>
		</tr>
		"""
	count++
}

sb += '''
</tbody>
</table>	
</div>
'''

if(tagTotal.size() > 0) {
	sb += """
	<script>
		var tagTotalData = new google.visualization.DataTable();
		tagTotalData.addColumn('string', 'Tag');
		tagTotalData.addColumn('number', 'Amount');
		tagTotalData.addRows(${tagTotal.size()});
	"""
	def i = 0
	tagTotal.each { k, v ->
		v = String.format("%.2f", v)
		sb += """
		tagTotalData.setValue($i, 0, '$k[$v]');
		tagTotalData.setValue(${i}, 1, $v);
	"""
		i++
	}
	def height = tagTotal.size() * 40
	sb += """
		var tagTotalChart = new google.visualization.BarChart(document.getElementById('barChart-all'));
		tagTotalChart.draw(tagTotalData, {width: 800, height: ${height}, is3D: true, title: '', legend: 'none',  
		axisFontSize: 14, legendTextStyle: {color: 'black', fontName: 'Arial', fontSize: 14}, backgroundColor: {stroke:'white', fill:'white', strokeSize: 1},
	legendBackgroundColor: {stroke:'white', fill:'white', strokeSize: 0}});
	</script>
	"""	
}

println sb.toString().replaceAll('totalTagAmount', String.format("%.2f", total))

}